package com.orange.eduservice.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.orange.commonutils.Result;
import com.orange.eduservice.entity.EduTeacher;
import com.orange.eduservice.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author orange
 * @since 2024-03-09
 */
@Api("讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
public class EduTeacherController {

    //访问地址：http://localhost:8001/eduservice/teacher/findAll
    //把sevice注入
    @Autowired
    private EduTeacherService teacherService;

    //1 查询讲师表中所有数据
    //rest风格
    @ApiOperation(value = "所有讲师列表")
    @GetMapping("findAll")
    public Result findAllTeacher() {
        //调用service方法实现查询所有的操作
        List<EduTeacher> list = teacherService.list(null);
        return Result.ok().data("items", list);
    }

    //2.逻辑删除讲师的方法 通过路径传id值
    @ApiOperation(value = "逻辑删除讲师")
    @DeleteMapping("{id}")
    public Result removeById(@ApiParam(name = "id", value = "讲师ID", required = true) @PathVariable String id) {
        boolean b = teacherService.removeById(id);
        if (b) {
            return Result.ok();
        }
        return Result.error();
    }

    /**
     * 3.分页查询讲师的方法
     *
     * @param current 当前页
     * @param limit   每页显示记录数
     * @return
     */
    @ApiOperation(value = "分页讲师列表")
    @GetMapping("pageTeacher/{current}/{limit}")
    public Result pageListTeacher(@ApiParam(name = "current", value = "当前页", required = true) @PathVariable Long current,
                                  @ApiParam(name = "limit", value = "每页显示记录数", required = true) @PathVariable Long limit) {
        //创建page对象
        Page<EduTeacher> pageTeacher = new Page<>(current, limit);
        //调用方法实现分页
        //调用方法的时候底层封装，把分页所有数据封装到pageTeacher对象里面
        teacherService.page(pageTeacher, null);
        long total = pageTeacher.getTotal();//总记录数
        List<EduTeacher> records = pageTeacher.getRecords();//数据list集合

        HashMap<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("rows", records);

        //return Result.ok().data("total", total).data("rows", records);
        return Result.ok().data(map);
    }
}

